<?php
namespace app\api\controller;

use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\library\Upload;
use fast\Random;
use think\Config;
use think\Db;
use think\Validate;

class Neworder extends Api{

    protected $noNeedLogin = ['uploadimg'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];



    /**
     * 我的案例
     */
    public function getmycases(){

        $uid=$this->auth->id;
        $page=$this->request->param('page',1,'intval');

        $map['id'] = [ 'gt', 0];
        $map['new'] = [ 'eq', 1];
        $startime = $this->request->param('startime');
        if ($startime) {
            $map['time'] = [ 'gt', strtotime($startime)];
        }

        $endtime = $this->request->param('endtime');
        if ($endtime) {
            $map['time'] = [ 'lt', strtotime($endtime)];
        }

        $merch = $this->request->param('merch');
        if ($merch) {
            $map['merch'] = [ 'eq', $merch];
        }


        $orde = $this->request->param('order', '0');
        $order = 'id desc';
        if ($orde == 1) {
            $order = 'endtimes asc';
        }
        if ($orde == 2) {
            $order = 'count desc';
        }
        if ($orde == 3) {
            $order = 'count asc';
        }
        if ($orde == 4) {
            $order = 'id desc';
        }

        $lists=Db::name('aa_case')
            ->where($map)
            ->page($page, 10)
            ->order($order)
            ->select();


        foreach ($lists as $k=>$v){


            $lasttime = Db::name('aa_photolpg_log')->where('cid', 'eq', $v['id'])->order('id desc')->find();

            if ($lasttime) {
                $lists[$k]['startime'] =$lasttime['time'];
            }else{
                $lists[$k]['startime'] =date('Y-m-d H:i',$v['time'] );
            }

            $count1=Db::name('newphoto')
                ->where('caseid', 'eq', $v['id'])
                ->count();

            $count2=Db::name('newphoto')
                ->where('caseid', 'eq', $v['id'])
                ->where('typein', 'eq', 1)->count();

            $count3=Db::name('newphoto')
                ->where('caseid', 'eq', $v['id'])
                ->where('typeok', 'eq', 1)
                ->count();
            //未拍完
            $count4=Db::name('newphoto')
                ->where('caseid', 'eq', $v['id'])
                ->where('typesave', 'eq', 0)
                ->where('typetie', 'eq', 1)
                ->where('typecontroversy', 'eq', 1)
                ->count();
            //争议
            $count5=Db::name('newphoto')
                    ->where('caseid', 'eq', $v['id'])
                    ->where('typecontroversy','eq',1)
                    ->count();

            $count6=Db::name('newphoto')
                ->where('caseid', 'eq', $v['id'])
                ->where('typetie', 'eq', 1)
                ->count();

            $count7=Db::name('newphoto')
                ->where('caseid', 'eq', $v['id'])
                ->where('typeout', 'eq', 1)
                ->count();

            $lists[$k]['count1'] =$count1;
            $lists[$k]['count2'] =$count2;
            $lists[$k]['count3'] =$count3;
            $lists[$k]['count4'] =$count4;
            $lists[$k]['count5'] =$count5;
            $lists[$k]['count6'] =$count6;
            $lists[$k]['count7'] =$count7;

            $lists[$k]['plan_v'] = getnewtypes($v['plan']);
        }


        $this->success('我的案例!', $lists);

    }


    /**
     * 我的案例
     */
    public function getmycasesb(){

        $uid=$this->auth->id;
        $page=$this->request->param('page',1,'intval');

        $map['new'] = [ 'eq', 1];

        $merch = $this->request->param('qid');
        if ($merch) {
            $map['q_id'] = [ 'eq', $merch];
        }

        $order = 'id desc';


        $lists=Db::name('aa_case')
            ->where($map)
            ->page($page, 10)
            ->order($order)
            ->select();


        foreach ($lists as $k=>$v){


            $lasttime = Db::name('aa_photolpg_log')->where('cid', 'eq', $v['id'])->order('id desc')->find();

            if ($lasttime) {
                $lists[$k]['startime'] =$lasttime['time'];
            }else{
                $lists[$k]['startime'] =date('Y-m-d H:i',$v['time'] );
            }

            $count1=Db::name('newphoto')
                ->where('caseid', 'eq', $v['id'])
                ->count();

            $count2=Db::name('newphoto')
                ->where('caseid', 'eq', $v['id'])
                ->where('typein', 'eq', 1)->count();

            $count3=Db::name('newphoto')
                ->where('caseid', 'eq', $v['id'])
                ->where('typesave', 'eq', 1)
                ->count();
            //未拍完
            $count4=Db::name('newphoto')
                ->where('caseid', 'eq', $v['id'])
                ->where('typesave', 'eq', 0)
                ->where('typetie', 'eq', 1)
                ->where('typecontroversy', 'eq', 1)
                ->count();
            //争议
            $count5=Db::name('newphoto')
                ->where('caseid', 'eq', $v['id'])
                ->where('typecontroversy','eq',1)
                ->count();

            $count6=Db::name('newphoto')
                ->where('caseid', 'eq', $v['id'])
                ->where('typetie', 'eq', 1)
                ->count();

            $count7=Db::name('newphoto')
                ->where('caseid', 'eq', $v['id'])
                ->where('typeout', 'eq', 1)
                ->count();

            $lists[$k]['count1'] =$count1;
            $lists[$k]['count2'] =$count2;
            $lists[$k]['count3'] =$count3;
            $lists[$k]['count4'] =$count4;
            $lists[$k]['count5'] =$count5;
            $lists[$k]['count6'] =$count6;
            $lists[$k]['count7'] =$count7;

            $lists[$k]['plan_v'] = getnewtypes($v['plan']);
        }


        $this->success('我的案例!', $lists);

    }


    /**
     * 我的案例
     */
    public function getmycasesc(){

        $uid=$this->auth->id;


        $merch = $this->request->param('qid');
        if ($merch) {
            $map['quotation_id'] = [ 'eq', $merch];
        }

        $order = 'id desc';


        $lists=Db::name('quotation_log_history')
            ->where($map)

            ->order('id desc')
            ->select();


        foreach ($lists as $k=>&$v){


            $v['addtime']=date('Y-m-d',$v['addtime']);


        }


        $this->success('我的案例!', $lists);

    }


    /**
     * 进行中 完成数量
     */
    public function getcasescount(){

        $count1=Db::name('aa_case')
            ->where('new','eq',1)
            ->where('plan','lt',3)->count();

        $year=$this->request->param('year');

        if (!$year){
            $year=date('Y');
        }

        $str=date('Y-m-d',strtotime($year.'-01-01'));
        $end=date('Y-m-d',strtotime($year.'-12-31'));


        $count2=Db::name('aa_case')
//            ->where('plan','gt',2)
            ->where('time','gt',strtotime($str))
            ->where('time','lt',strtotime($end))
            ->where('new','eq',1)
            ->count();

        $this->success('我的案例!', ['num1'=>$count1,'num2'=>$count2,'year'=>$year]);


    }

    /**
     * 查询 详情
     */
    public function getgoodbyid()
    {
        $code=$this->request->param('code');

        $id=$this->request->param('id');

        if  ($id){
            $infos = Db::name('newphoto')
                ->where('id', 'like', '%'. $id.'%')
                ->limit('6')
                ->select();
        }else{

            $infos = Db::name('newphoto')
                ->where('code', 'like','%'. $code.'%')
                ->whereOr('sku', 'like', '%'.$code.'%')
                ->whereOr('title', 'like', '%'.$code.'%')
                ->limit('6')
                ->select();
        }




        if (count($infos)<1){
            $this->error('没有相关信息!');
        }


        if (count($infos)>1){
            $pc=Db::name('aa_case')->where(['id'=>$infos[0]['caseid']])->find();

            if (!$pc){
                $pc['title']='';
            }

    //            var_dump($infos);
    //            die;
            $array=[];
            foreach ($infos as &$kk){
                $arr=[];
                $arr['case']=$pc['title'];
                $arr['id']=$kk['id'];
                $tx=$kk['typeout']==1?' 退仓':' 未退';
                $arr['title']=$pc['title'].'-'.$kk['color1'].$tx ;
                $array[]=$arr;
            }

            $this->success('多个信息',$array);
        }else{
            $info=$infos[0];
        }


        $case=Db::name('aa_case')->where(['id'=>$info['caseid']])->find();
        $info['case']=$case['title'];

        $sku=explode('-',$info['sku']);

        if (is_array($sku)){
            $skustr=$sku[0];
            if(isset($sku[1])&&count($sku)>2){
                $skustr.='-'.$sku[1];
            }
        }else{
            $skustr='';
        }


        $tongkuan=Db::name('newphoto')
            ->where('sku','like','%'.$skustr.'%')
            ->where('caseid','eq',$info['caseid'])
            ->field(['sku','code','color1','createtime','typeout'])->select();

        $tktit=[];
        foreach ($tongkuan as $key){
            $tx=$key['typeout']==1 ? ' 退仓' : ' 未退';
            $tktit[]=$key['sku']." ".mb_substr($key['color1'],0,9) . $tx;
        }

        $info['status_v']=getnewphpototypes($info);

        $info['tongkuan']=$tongkuan;
        $info['tktit']=$tktit;
        $info['tongkuancount']=count($tongkuan);



        $more=unserialize($info['more']);

        $info['more']=$more;

        $this->success('请求成功!', $info);


    }



    /**
     *上传图片
     */
    public function uploadimg(){

        $id=$this->request->param('id');
        $uid=$this->request->param('uid');

        $file = $this->request->file('file');
        $upload = new Upload($file);
        $attachment = $upload->upload();


        $imgurl=cdnurl($attachment->url, true);

        Db::name('newphoto')
            ->where(['id'=>$id])
            ->update(['img'=>$imgurl,'typein'=>1,'typesave'=>1]);

        $infos=Db::name('newphoto')->where(['id'=>$id])->find();

        $user=Db::name('user')->where(['id'=>$uid])->find();

        $logs['name']=$user['nickname'];
        $logs['desc']='修改状态为:入库';
        $logs['time']=date('Y-m-d H:i');
        $logs['pid']=$id;
        $logs['uid']=$user['id'];
        $logs['cid']=$infos['caseid'];

        Db::name('aa_photolpg_log')->data($logs)->insert();



        //入库完成比大于50  则为制作中
        $isnoin=Db::name('newphoto')
            ->where('caseid','eq',$infos['caseid'])
            ->where('typein','eq',1)
            ->count();

        $isnoin2=Db::name('newphoto')
            ->where('caseid','eq',$infos['caseid'])
            ->count();

        if (intval($isnoin*2)>$isnoin2){
            Db::name('aa_case')
                ->where('id','eq',$infos['caseid'])
                ->update(['plan'=>2]);
        }

        //如果都是不拍摄 则修改为  完成和搭配
        if  ($infos['typeok']!=1){
            $this->setdapeibyid($id);
        }


        $this->success("恭喜您,API访问成功!", ['img'=>$imgurl]);
    }


    /**
     *Title:只有入库中的才修改成搭配
     *<br/>By: 王龙 2022/8/29 17:23
     */
    public function setdapeibyid($id){

        $info=Db::name('newphoto')
            ->where(['id'=>$id])
            ->find();

        if ($info['typetie']==1||$info['typecontroversy']==1) {
            Db::name('newphoto')->where(['id'=>$id])
                ->update(['typeok'=>1,'typesave'=>1]);
        }

    }




    /**
     * 可转换订单
     * */
    public function getcanzh(){

        $id=$this->request->param('id');

        $info=Db::name('newphoto')->where(['id'=>$id])->find();

        $case=Db::name('aa_case')->where(['id'=>$info['caseid']])->find();

        $list=Db::name('aa_case')
            ->where(['merch'=>$case['merch']])
            ->order('id desc')
            ->limit(6)
            ->select();


        $this->success('可转换订单!',$list);
    }





    /**
     * 备注
     * */
    public function  setgooddesc(){

        $code=$this->request->param('id');

        $desc=$this->request->param('desc');

        Db::name('newphoto')->where(['id'=>$code])->update(['good_desc'=>$desc]);

        $this->success('请求成功!');
    }



    /**
     *  操作记录
     */
    public function getlogsbyid(){

        $id=$this->request->param('id');

        $list=Db::name('aa_photolpg_log')
            ->where(['pid'=>$id])
            ->order('id desc')
            ->select();

        $this->success('操作记录！!',$list);
    }

    /**
     * 进行操作  结算
     */
    public function setphpotoinfo(){

        $id=$this->request->param('id');
        $index=$this->request->param('index');
        $user=$this->auth->getUser();

        $info=Db::name('newphoto')->where(['id'=>$id])->find();


        if ($info['typein']!=1){
            $this->error('未入库!');
        }


        $more=unserialize($info['more']);

        $more[$index]['status']=1;

        Db::name('newphoto')->where(['id'=>$id])->update(['more'=>serialize($more)]);


        $logs['name']=$user['nickname'];
        $logs['desc']= '工作：'.$more[$index]['title'] . '已经完成';
        $logs['time']=date('Y-m-d H:i');
        $logs['pid']=$id;
        $logs['uid']=$user['id'];
        $logs['cid']=$info['caseid'];

        Db::name('aa_photolpg_log')->data($logs)->insert();

        $gid=$more[$index]['gid'];

       $cle= Db::name('clearing_log')
            ->where(['catefrolist_id'=>$gid,'caseid'=>$info['caseid']])

            ->find();


       $set['number']=$cle['number']+1;
       $set['allmoney']=$cle['allmoney']+$cle['money'];

        Db::name('clearing_log')
            ->where(['id'=>$cle['id']])
            ->update($set);

        $ok=1;
        foreach ($more as $key){

            if ($key['set']=='是'&&$key['status']==0){
                $ok=0;
            }
        }


        if ($ok!=0){
            Db::name('newphoto')->where(['id'=>$id])->update(['typeok'=>1]);
        }

        $all=Db::name('newphoto')->where(['typeok'=>0,'caseid'=>$info['caseid']])->find();


        if (!$all){
            Db::name('aa_case')->where(['id'=>$info['caseid']])->update(['plan'=>3]);
        }
        $this->success('操作成功!');

        $this->success('ok');

    }

    //异常
    public function setphpotocontroversy(){

        $id=$this->request->param('id');

        $user=$this->auth->getUser();

        $info=Db::name('newphoto')->where(['id'=>$id])->find();

        if ($info['typein']!=1){
            $this->error('未入库!');
        }

        Db::name('newphoto')->where(['id'=>$id])->update(['typecontroversy'=>1]);


        $logs['name']=$user['nickname'];
        $logs['desc']= '设置为异常';
        $logs['time']=date('Y-m-d H:i');
        $logs['pid']=$id;
        $logs['uid']=$user['id'];
        $logs['cid']=$info['caseid'];

        Db::name('aa_photolpg_log')->data($logs)->insert();


        Db::name('newphoto')->where(['id'=>$id])->update(['typeok'=>1]);


        $all=Db::name('newphoto')->where(['typeok'=>0,'caseid'=>$info['caseid']])->find();


        if (!$all){
            Db::name('aa_case')->where(['id'=>$info['caseid']])->update(['plan'=>3]);
        }


    }

    /**
     * 修改信息
     */
    public function setgoodcodebyid(){

        $code=$this->request->param('code');
        $id=$this->request->param('id');

        $info=Db::name('newphoto')->where(['id'=>$id])->find();

        if (!$info){
            $this->error('操作失败');
        }

        Db::name('newphoto')->where(['id'=>$id])->update(['code'=>$code,'setcode'=>1]);

        $user=$this->auth->getUser();


        $logs['name']=$user['nickname'];
        $logs['desc']='修改了code';
        $logs['time']=date('Y-m-d H:i');
        $logs['pid']=$info['id'];
        $logs['uid']=$user['id'];

        $logs['cid']=$info['caseid'];
        Db::name('aa_photolpg_log')->data($logs)->insert();

        $this->success('操作成功!');


    }


}